/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import Login.MySqlConnector;
import Registration.RegistrationBean;
import java.io.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Bharath
 */
public class ViewMember extends HttpServlet {

    MySqlConnector ms;
    ViewMemberDao vmd;
    EditMemberDao emd;
    ManageMembersDao mmd;
    private String rcheck;

    public ViewMember() {
        ms = new MySqlConnector();
        vmd = new ViewMemberDao();
        mmd = new ManageMembersDao();
    // emd = new EditMemberDao();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            rcheck = request.getParameter("rcheck");
            System.out.println("rcheck is ---------->" + rcheck);
            //rcheck in session\\
            HttpSession hs = request.getSession(true);
            hs.setAttribute("view_rcheck", rcheck);
            System.out.println("rcheck after set session is ---------- > " + rcheck);

/*************************  View Members ************************************/

            if (request.getParameter("ViewMem") != null) {
                try
                {
                System.out.println(" View Member button clicked ");
                RegistrationBean status = vmd.view_member(rcheck);
                System.out.println("******"+status);
                //InputStream istream = (InputStream) status.elementAt(41);                
                if (status != null)
                {
                    //response.setContentType("image/jpg");
                    //response.getOutputStream().write(istream,0,istream.available());
                    //response.getOutputStream().flush();
                    System.out.println("in status true");
                    request.setAttribute("rbeanview", status);
                    request.getRequestDispatcher("/FullView.jsp").forward(request, response);
                }
                }catch(Exception e){e.printStackTrace();}
            }//End ViewMember

            /*****************************  Edit Members ************************************/
            if (request.getParameter("EditMem") != null) {
                System.out.println("in Edit Member");
                RegistrationBean status1 = vmd.view_member(rcheck);
                System.out.println("status in edit member" + status1);
                if (status1 != null) {
                    System.out.println("in edit - status true ");
                    request.setAttribute("rbeanedit", status1);
                    request.getRequestDispatcher("/EditMember.jsp").forward(request, response);
                }
            }

            /*******************  Delete Members ***********************************************/
            if (request.getParameter("DeleteMem") != null) {
                System.out.println(" Delete Member button clicked ");
                boolean b = mmd.Delete_Member(rcheck);
                if (b == true) {
                    
                    System.out.println("in b==true condition " + b);
                    out.print("<script language = 'javascript'>alert('Member deleted succesfull'); location='AllMembers.jsp'</script>");
                //request.getRequestDispatcher("/AllMembers.jsp").forward(request, response);
                }
            }//End DeleteMember


        } catch (Exception e) {
            
            e.printStackTrace();

            System.out.print("Exception is :" + e);
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ViewMember.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ViewMember.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ViewMember.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ViewMember.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
